package com.situ.orderingsystem.dao.impl;

import com.situ.orderingsystem.dao.OrderDao;
import com.situ.orderingsystem.global.Global;
import com.situ.orderingsystem.model.Employee;
import com.situ.orderingsystem.model.Order;
import com.situ.orderingsystem.model.OrderSearchBean;
import com.situ.orderingsystem.service.OrderService;
import com.situ.orderingsystem.utils.PaginateInfo;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    private  final JdbcTemplate template = new JdbcTemplate(Global.getDataSource());

    @Override
    public List<Order> findAll(OrderSearchBean es, PaginateInfo pi) {
        String countsql ="select count(0) from `order` t1";
        String sql = "select t1.id,t1.ord_id,t1.emp_id,t2.`name` as empName,t1.food_id,t3.`name` as foodName from `order` t1  " +
                "LEFT JOIN employee t2  on  t1.emp_id = t2.emp_id " +
                "LEFT JOIN food t3 " +
                "on t1.food_id  = t3.food_id";
        List<Object> objectList = new ArrayList<>();
        StringBuilder whysql = new StringBuilder();
        if(es != null){
            whysql.append(" where 1 = 1 and t1.is_deleted = 0");
            if(es.getEmpId() != null){
                whysql.append(" and emp_id = ?");
                objectList.add(es.getEmpId());
            }
            if(es.getOrdId()!=null){
                whysql.append(" and ord_id = ?");
                objectList.add(es.getOrdId());
            }
            if(StringUtils.hasText(es.getFoodId())){
                whysql.append(" and food_id = ?");
                objectList.add(es.getFoodId());
            }
        }

        countsql += whysql.toString();
        Long count = template.queryForObject(countsql,Long.class,objectList.toArray());
        pi.setCount(count);
        whysql.append(" limit ?, ?");
        objectList.add(pi.getOffset());
        objectList.add(pi.getLimit());
        sql+=whysql;

        List<Order> list = template.query(sql,new BeanPropertyRowMapper<>(Order.class),objectList.toArray());
        return list;
    }
    public List<Order> findAll() {
        String sql = "select id,ord_id,emp_id,food_id from `order`";
        List<Order> list = template.query(sql,new BeanPropertyRowMapper<>(Order.class));
        return list;
    }

    @Override
    public boolean deleteById(Integer id) {
        String sql ="update `order` set is_deleted = 1 where id = ?";
        int rows = template.update(sql,id);
        return  rows>0;

    }

    @Override
    public int add(Order order) {
        String sql = "insert into `order`" +
                "(ord_id,emp_id,food_id)" +
                "values (?,?,?)";
        int rows = template.update(sql,order.getOrdId(),order.getEmpId(),order.getFoodId());
        return rows;
    }

    @Override
    public Order searchById(Integer id) {
        String sql = "select ord_id,emp_id,food_id from `order` where id = ?";
        List<Order> emp = template.query(sql,new BeanPropertyRowMapper<>(Order.class),id);
        return emp.get(0);
    }

    @Override
    public int update(Order order) {
        String sql ="update `order` set ord_id = ? , emp_id = ? , food_id = ?  where id = ?";
        int rows = template.update(sql,order.getOrdId(),order.getEmpId(),order.getFoodId(),order.getId());
        return  rows ;

    }
}
